package com.onlineMIS.ORM.DAO.chainS.inventoryFlow;



import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;
import com.onlineMIS.ORM.DAO.BaseDAO;
import com.onlineMIS.ORM.entity.chainS.inventoryFlow.ChainInventoryDTO;
import com.onlineMIS.ORM.entity.chainS.inventoryFlow.ChainInventoryFlowOrderProduct;
import com.onlineMIS.common.Common_util;




@Repository
public class ChainInventoryFlowOrderProductDaoImpl extends BaseDAO<ChainInventoryFlowOrderProduct> {
	@SuppressWarnings("unchecked")
	public void deleteProducts(final List<Integer> ids) {
		if (ids != null && ids.size() > 0){
			String parameter = "?";
			for (int i =1;i<ids.size();i++)
				parameter +=",?";
			final String hql = "delete from ChainInventoryFlowOrderProduct p where p.id in ("+parameter+")";
			
			Object[] values = new Object[ids.size()];
			for (int i = 0; i < ids.size(); i++){
				values[i] = ids.get(i);
			}
			executeHQLUpdateDelete(hql, values, true);
			
		
		}
	}

	/**
	 * 根据单据id集合查询货品信息
	 * @param orderIds
	 * @return
	 */
	public List<ChainInventoryDTO> selectByOrderIds(List<Integer> orderIds) {
		List<ChainInventoryDTO> list = new ArrayList<ChainInventoryDTO>();
		String hql = "select productBarcode.id, sum(quantity) from ChainInventoryFlowOrderProduct where flowOrder.id in (";
		if (orderIds.size() != 0) {
			for (int i = 0; i < orderIds.size(); i++) {
				if (i == orderIds.size() - 1) {
					hql += orderIds.get(i) + ") group by productBarcode.id";
				} else {
					hql += orderIds.get(i) + ",";
				}
			}
		}
		List<Object> values = this.executeHQLSelect(hql, null, null, true);
		if (values.size() != 0) {
			for (Object record : values) {
				ChainInventoryDTO dto = new ChainInventoryDTO();
				Object[] recordResult = (Object[]) record;
				dto.setProductId(Common_util.getInt(recordResult[0]));
				dto.setSumQuantity(Common_util.getInt(recordResult[1]));
				list.add(dto);
			}
		}
		return list;
	}

}
